﻿using SmartMotion.Core;
using System;

namespace E5032Lib
{
    public class Axis : AxisBase
    {
        public Axis()
        {

        }
        public Axis(ushort cardID, ushort axisID) : base(cardID, axisID)
        {
        }

        public override int SetEquiv(double equiv)
        {
            return LTDMC.dmc_get_equiv(CardID, AxisID, ref equiv);
        }

        public override int GetEquiv(out double equiv)
        {
            equiv = 0;
            return LTDMC.dmc_get_equiv(CardID, AxisID, ref equiv);
        }

        public override int Stop(ushort stopMode)
        {
            return LTDMC.dmc_stop(CardID, AxisID, stopMode);
        }

        public override int AxisEnable()
        {
            return LTDMC.nmc_set_axis_enable(CardID, AxisID);
        }

        public override int AxisDisable()
        {
            return LTDMC.nmc_set_axis_disable(CardID, AxisID);
        }

        public override int SetHomeParms(ushort homeMode, double lowVel, double highVel, double acc, double dec, double offsetPos)
        {
            return LTDMC.nmc_set_home_profile(CardID, AxisID, homeMode, lowVel, highVel, acc, dec, offsetPos);
        }

        public override int GetHomeParms(out ushort homeMode, out double lowVel, out double highVel, out double acc, out double dec,
            out double offsetPos)
        {
            homeMode = 0;
            lowVel = 0;
            highVel = 0;
            acc = 0;
            dec = 0;
            offsetPos = 0;
            return LTDMC.nmc_get_home_profile(CardID, AxisID, ref homeMode, ref lowVel, ref highVel, ref acc, ref dec, ref offsetPos);
        }

        public override int HomeMove()
        {
            return LTDMC.nmc_home_move(CardID, AxisID);
        }

        public override int GetHomeResult(out ushort state)
        {
            state = 0;
            return LTDMC.dmc_get_home_result(CardID, AxisID, ref state);
        }

        public override int SetSpeed(double minVel, double maxVel, double acc, double dec, double stopVel)
        {
            return LTDMC.dmc_set_profile_unit(CardID, AxisID, minVel, maxVel, acc, dec, stopVel);
        }

        public override int SetSpeed(Speed speed)
        {
            return LTDMC.dmc_set_profile_unit(CardID, AxisID, speed.MinVel, speed.MaxVel, speed.Acc, speed.Dec, speed.StopVel);
        }

        public override int SetSpeed(double rate)
        {
            SpeedRate = rate;
            return LTDMC.dmc_set_profile_unit(CardID, AxisID, MinVel * rate, MaxVel * rate, MaxAcc * rate, MaxDec * rate, MaxDec * rate);
        }

        public override int GetSpeed(out double minVel, out double maxVel, out double acc, out double dec, out double stopVel)
        {
            minVel = 0;
            maxVel = 0;
            acc = 0;
            dec = 0;
            stopVel = 0;
            return LTDMC.dmc_get_profile_unit(CardID, AxisID, ref minVel, ref maxVel, ref acc, ref dec, ref stopVel);
        }

        public override int GetSpeed(out Speed speed)
        {

           int ret= GetSpeed(out double minVel, out double maxVel, out double acc, out double dec, out double stopVel);
           speed.MinVel = minVel;
           speed.MaxVel = maxVel;
           speed.Acc = acc;
           speed.Dec = dec;
           speed.StopVel = stopVel;

           return ret;
        }

        public override int GetSpeed(out double rate)
        {
            rate = SpeedRate;
            return 0;
        }

        public override int Move(double dist, ushort posiMode)
        {
            return LTDMC.dmc_pmove(CardID, AxisID, (int)dist, posiMode);
        }

        public override int CheckDone()
        {
            return LTDMC.dmc_check_done(CardID, AxisID);
        }

        public override int VMove(ushort dir)
        {
            return LTDMC.dmc_vmove(CardID, AxisID, dir);
        }

        public override int TMove(double torque, double maxSpeed)
        {
            throw new NotImplementedException();
        }
    }
}
